const fs = require("fs");
const readline = require("readline");
const path = require("path");
// const file = path.resolve(__dirname, "../w3c/week1/1_2/1_2_1/1.md");

const glob = require("glob");
const list = glob.sync(`../w3c/**/*.srt`);

async function dolist() {
  for await (const v of list) {
    console.log("tran sub:" + v);
    await tranSub(v);
  }
}
dolist();

async function tranSub(file) {
  let i = 0;

  const input = fs.createReadStream(file);
  let output = ["WEBVTT\n\n"];
  const rl = readline.createInterface({
    input: input,
    crlfDelay: Infinity,
  });
  // 字幕单元行号
  let iUnit = 1;
  for await (let line of rl) {
    if (i === parseInt(line)) {
    //   console.log("num:", parseInt(line));
      i++;
      iUnit = 1;
      output.push("\n" + line);
    } else {
      iUnit++;
      // 时间轴
      if (iUnit === 2) {
        line = line.replace(",", ".");
        line = line.replace(",", ".");
        // console.log('Time',line)
        output.push(line);
      }

      // 删除英文 第三行
      if (iUnit === 3) {
        // console.log('English',line)
        // output.push(line)
      }
      if (iUnit === 4) {
        // 中文
        output.push(line);
      }
    }
  }
  fs.writeFileSync(file.replace(".srt", ".vtt"), output.join("\n"));
}

tranSub("../w3c/week1/1_1/1.srt");
